package com.sky.controller.User;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/user/user")
@Slf4j
@Api(tags = "微信小程序登录")
public class Usercontroller {
    @Autowired
    UserService userService;
    @Autowired
    JwtProperties jwtProperties;
    @PostMapping("/login")
    public Result<UserLoginVO> userlogin(@RequestBody UserLoginDTO userLoginDTO)
    {log.info("微信登录code为：{}",userLoginDTO.getCode());
        UserLoginVO userLoginVO = userService.wechatlogin(userLoginDTO);
        //设置jwt
        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID,userLoginVO.getId());
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), map);
        log.info("创建的jwt为{}",jwt);
        userLoginVO.setToken(jwt);
        return Result.success(userLoginVO);
    }


}
